<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="utf-8">
    <title>编辑NC服务器</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
</head>
<body class="pear-container">

<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">编辑NC服务器</div>
        <div class="layui-card-body">
            <form class="layui-form" lay-filter="form">
                <input type="hidden" name="id" value="">

                <div class="layui-form-item">
                    <label class="layui-form-label">服务器名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="server_name" required lay-verify="required" placeholder="请输入服务器名称" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">客户编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="customer_number" required lay-verify="required" placeholder="请输入NC客户编号" class="layui-input">
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">SCP密码</label>
                    <div class="layui-input-block">
                        <input type="password" name="scp_password" value="••••••••" placeholder="留空则不修改密码" class="layui-input">
                        <div class="layui-form-mid layui-word-aux">留空则保持原密码，如需修改请重新输入完整密码</div>
                    </div>
                </div>

                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="1" title="启用" checked>
                        <input type="radio" name="status" value="0" title="禁用">
                    </div>
                </div>

                <div class="layui-form-item layui-form-text">
                    <label class="layui-form-label">备注</label>
                    <div class="layui-input-block">
                        <textarea name="notes" placeholder="请输入备注信息" class="layui-textarea"></textarea>
                    </div>
                </div>

                <div class="layui-form-item">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="update">立即提交</button>
                        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>

<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script>
    layui.use(['form', 'jquery'], function () {
        var form = layui.form;
        var $ = layui.jquery;

        // 获取URL参数中的id
        var urlParams = new URLSearchParams(window.location.search);
        var id = urlParams.get('id');

        if (id) {
            // 加载数据
            loadData(id);
        }

        // 加载数据
        function loadData(id) {
            var loading = layer.load();

            $.ajax({
                url: '/app/csauto/ncservers/select',
                type: 'GET',
                data: { id: id },
                dataType: 'json',
                success: function (res) {
                    layer.close(loading);
                    if (res.code === 0 && res.data.length > 0) {
                        var item = res.data[0];

                        // 填充表单数据
                        $('input[name="id"]').val(item.id);
                        $('input[name="server_name"]').val(item.server_name);
                        $('input[name="customer_number"]').val(item.customer_number);
                        $('textarea[name="notes"]').val(item.notes);

                        // 设置状态单选框
                        $('input[name="status"][value="' + item.status + '"]').prop('checked', true);

                        form.render();
                    } else {
                        layer.msg('数据加载失败', {icon: 2});
                    }
                },
                error: function () {
                    layer.close(loading);
                    layer.msg('网络错误，请稍后重试', {icon: 2});
                }
            });
        }

        // 表单提交
        form.on('submit(update)', function (data) {
            var formData = data.field;

            // 如果密码是默认的隐藏值，则从表单中移除
            if (formData.scp_password === '••••••••') {
                delete formData.scp_password;
            }

            var loading = layer.load();

            $.ajax({
                url: '/app/csauto/ncservers/update',
                type: 'POST',
                data: formData,
                dataType: 'json',
                success: function (res) {
                    layer.close(loading);
                    if (res.code === 0) {
                        layer.msg(res.msg, {icon: 1});
                        // 延迟一点时间让用户看到成功消息，然后关闭弹窗
                        setTimeout(function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));
                            parent.window.refreshTable();
                        }, 1000);
                    } else {
                        layer.msg(res.msg, {icon: 2});
                    }
                },
                error: function () {
                    layer.close(loading);
                    layer.msg('网络错误，请稍后重试', {icon: 2});
                }
            });

            return false; // 阻止默认提交
        });
    });
</script>

</body>
</html>
